import {createSlice} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";
import {
    attrItemType, reqAddOrUpdateAttrList,
    reqAttrInfoList,
    reqCategory1List,
    reqCategory2List,
    reqCategory3List,
    reqDeleteAttr
} from "@/api/goods/attr";
import {message} from "antd";

const attrSlice = createSlice({
    name:'atrr',
    initialState:{
        //三级分类的列表
        category1List: [],
        category2List: [],
        category3List: [],
        //三级分类每一级的id
        category1Id: null,
        category2Id: null,
        category3Id: null,
    //    平台属性列表
        attrList:[]
    },
    reducers:{
        setCategory1List(state,{payload}){
            state.category1List = payload
        },
        setCategory2List(state,{payload}){
            state.category2List = payload
        },
        setCategory3List(state,{payload}){
            state.category3List = payload
        },
        setCategory1Id(state,{payload}){
            state.category1Id = payload
        },
        setCategory2Id(state,{payload}){
            state.category2Id = payload
        },
        setCategory3Id(state,{payload}){
            state.category3Id = payload
        },
        setAttrList(state,{payload}){
            state.attrList = payload
        }
    }
})

const {setCategory1List,setCategory2List,setCategory3List,
    setCategory1Id,setCategory2Id,setCategory3Id,setAttrList} = attrSlice.actions

//暴露state
export const useSelectAttr = ()=>useSelector((state:any)=>state.attr)

//一级分类变化，二级分类和三级分类id和数据清空；二级分类变化，三级分类id和数据清空
//获取一级分类
export const getCategory1ListAsync = ()=>{
    return async (dispatch:any)=>{
        const res:any = await reqCategory1List()
        dispatch(setCategory1List(res.data))
    }
}
//获取二级分类
export const getCategory2ListAsync = (category1Id:any)=>{
    return async (dispatch:any)=>{
        const res:any = await reqCategory2List(category1Id)
        dispatch(setCategory2List(res.data))
        dispatch(setCategory1Id(category1Id))
    }
}
//获取三级分类
export const getCategory3ListAsync = (category2Id:number)=>{
    return async (dispatch:any)=>{
        const res:any = await reqCategory3List(category2Id)
        dispatch(setCategory3List(res.data))
        dispatch(setCategory2Id(category2Id))
    }
}
//根据三级分类id获取平台属性
export const getAttrInfoListAsync = (category3Id:number)=>{
    return async (dispatch:any,getState:any)=>{
        dispatch(setCategory3Id(category3Id))
        const {category1Id,category2Id} = getState().attr
        const res:any = await reqAttrInfoList(category1Id,category2Id,category3Id)
        dispatch(setAttrList(res.data))
    }
}
//删除平台属性
export const deleteAttrAsync = (id:number)=>{
    return async (dispatch:any)=>{
        try {
            await reqDeleteAttr(id)
            message.success('删除成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}
//根据id删除某个平台属性值
export const deleteAttrValAsync = (id:number)=>{
    return async (dispatch:any)=>{
        try {
            await reqDeleteAttr(id)
            message.success('删除成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}
//提交新增属性或者修改属性
export const addOrUpdateAttrListAsync = (newAttr: attrItemType)=>{
    return async (dispatch:any)=>{
        try {
            await reqAddOrUpdateAttrList(newAttr)
            message.success('修改成功~')
        }catch (e:any) {
            message.error(e.message)
        }
    }
}

//暴露reducer
export default attrSlice.reducer
